﻿using System;

namespace SailHero.Soft.ESB.Communication.ClientAndServer.Communication.Messages
{
    /// <summary>
    /// Represents a message that is sent and received by server and client.
    /// This is the base class for all messages.
    /// </summary>
    [Serializable]
    public class Message : IMessage
    {
        /// <summary>
        /// Unique identified for this message.
        /// Default value: New GUID.
        /// Do not change if you do not want to do low level changes
        /// such as custom wire protocols.
        /// </summary>
        public string MessageId { get; set; }

        /// <summary>
        /// This property is used to indicate that this is
        /// a Reply message to a message.
        /// It may be null if this is not a reply message.
        /// </summary>
        public string RepliedMessageId { get; set; }


        /// <summary>
        /// 消息的发送者MN 长度为60字符
        /// </summary>
        public string FromMN { get; set; }

        /// <summary>
        /// 消息的接收者 长度为60字符
        /// </summary>
        public string To { get; set; }

        /// <summary>
        /// 消息的QN码，用来标识一次通话的过程，长度17字符，
        /// e.g. 20040516010101001
        /// </summary>
        public string QN
        {
            get;
            set;
        }
        /// <summary>
        /// 消息中的MN码，用来标识该次通话的现场机，
        /// e.g. 88888880000001
        /// </summary>
        public string MN
        {
            get;
            set;
        }

        /// <summary>
        /// 消息中MN码对应的密码
        /// </summary>
        public string PW
        {
            get;
            set;
        }
        /// <summary>
        /// 消息中CN
        ///  </summary>
        public string CN
        {
            get;
            set;
        }
        /// <summary>
        /// 消息中ST
        ///  </summary>
        public string ST
        {
            get;
            set;
        }
        /// <summary>
        /// 消息中Flag
        ///  </summary>
        public string Flag
        {
            get;
            set;
        }
        /// <summary>
        /// 消息中CP
        ///  </summary>
        public string CP
        {
            get;
            set;
        }
        /// <summary>
        /// 消息的类型 0 表示由上位机请求 1 表示由现场机发出的指令，
        /// </summary>
        public int MsgType
        {
            get;
            set;
        }
        /// <summary>
        /// Creates a new Message.
        /// </summary>
        public Message()
        {
            MessageId = Guid.NewGuid().ToString("N");
        }

        /// <summary>
        /// Creates a new reply Message.
        /// </summary>
        /// <param name="repliedMessageId">
        /// Replied message id if this is a reply for
        /// a message.
        /// </param>
        public Message(string repliedMessageId)
            : this()
        {
            RepliedMessageId = repliedMessageId;
        }

        /// <summary>
        /// Creates a string to represents this object.
        /// </summary>
        /// <returns>A string to represents this object</returns>
        public override string ToString()
        {
            return string.IsNullOrEmpty(RepliedMessageId)
                       ? string.Format("Message [{0}]", MessageId)
                       : string.Format("Message [{0}] Replied To [{1}]", MessageId, RepliedMessageId);
        }
    }
}
